﻿using daonaite.Model.system.dept;
using daonaite.services.system;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace daonaite.api.Controllers.system
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class DeptController : BaseController
    {
        private readonly deptService deptcz;

        public DeptController(deptService deptcz)
        {
            this.deptcz = deptcz;
        }

        /// <summary>
        /// 查询树形部门信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> gettreedept([FromBody] Dto_dept data)
        {
            var result = await deptcz.gettreedept(data);
            return Success(result);
        }

        /// <summary>
        /// 根据id查询部门信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> get(int id)
        {
            var result = await deptcz.getAsync(id);
            return Success(result);
        }

        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> add([FromBody] Dto_dept data)
        {
            sys_dept dept = new sys_dept();
            copy<Dto_dept, sys_dept>(data, dept,true);
            var result = await deptcz.add(dept);
            return Success(result);
        }

        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> update([FromBody] sys_dept data)
        {
            sys_dept dept = new sys_dept();
            copy<sys_dept, sys_dept>(data, dept, false,true);
            var result = await deptcz.updateAsync(dept); 
            return Success(result);
        }
        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> delete(int id)
        {
            var result = await deptcz.delete(id);
            return Success(result);
        }

    }
}
